<template>
	<view class="index bf">
		<view class="" style="margin:0 auto; ">
			<u-avatar :src="site+adminInfo.avatar" size="100"></u-avatar>
		</view>
		<view class="p10 f-1">
			<u--form labelPosition="top" :model="userInfo" :rules="rules" ref="uForm" labelWidth="100">
				<u-form-item label="用户名" prop="username" ref="item1">
					<u--input border="bottom" v-model="userInfo.username" readonly></u--input>
				</u-form-item>
				<u-form-item label="用户昵称" prop="nickname" ref="item1">
					<u--input placeholder="请输入用户昵称" border="bottom" v-model="userInfo.nickname"></u--input>
				</u-form-item>
				<u-form-item label="手机号码" prop="mobile" ref="item1">
					<u--input placeholder="请输入手机号码" border="bottom" v-model="userInfo.mobile"></u--input>
				</u-form-item>
				<u-form-item label="邮箱地址" prop="email" ref="item1">
					<u--input placeholder="请输入邮箱地址" border="bottom" v-model="userInfo.email"></u--input>
				</u-form-item>
				<u-form-item label="签名" prop="motto" ref="item1">
					<u--textarea v-model="userInfo.motto" placeholder="请输入签名"></u--textarea>
				</u-form-item>
				<!-- <u-form-item label="新密码" prop="password" ref="item1">
					<u--input placeholder="不修改请留空" border="bottom" v-model="userInfo.password"></u--input>
				</u-form-item> -->
			</u--form>
		</view>
		<!-- <view class="p10 f-1">
			<u-button size="large" :customStyle="{color:'#000',width:'80%',fontWeight:'bold',marginTop:'20rpx'}"
				color="linear-gradient(to right, #fee14f, #fdc450)" shape="circle" text="保存修改" type="info"
				@click="edit"></u-button>
		</view> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
	} from 'vuex'
	import COS from 'cos-wx-sdk-v5';
	import CryptoJS from 'crypto-js';
	import site from '@/custom/siteroot.js';
	export default {
		data() {
			return {
				cos: null, // COS SDK 实例
				show: false,
				form: {
					oldPassword: '',
					password: '',
					password_confirmation: '',
				},
				userInfo: {
					username: '',
					nickname: '',
					mobile: '',
					email: '',
					motto: '',
					password: ''
				},
				adminInfo: uni.getStorageSync("adminInfo"),
				rules: {
					nickname: [{
						required: true,
						message: '请填写',
						trigger: 'blur'
					}],
					mobile: [{
							required: true,
							message: '请填写',
							trigger: 'blur'
						},
						{
							min: 11,
							max: 11,
							message: '手机格式不正确',
							trigger: 'blur'
						},
					],
					email: [{
							required: true,
							message: '请填写',
							trigger: 'blur'
						},
						{
							min: 11,
							max: 11,
							type: 'email',
							message: '邮箱格式不正确',
							trigger: 'blur'
						},
					],
				},
			}
		},
		computed: {
			...mapState({
				user: state => state.user,
			}),
		},
		onLoad() {
			this.getAdminInfo()
		},
		mounted() {
			// 初始化 COS SDK
			this.initCOS();
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			...mapMutations(["setSiteroot"]),
			logOut() {
				uni.showModal({
					title: '提示',
					content: '请确认是否要清除缓存并退出吗?',
					success: async (res) => {
						if (res.confirm) {
							uni.removeStorageSync('token')
							uni.removeStorageSync('storeId')
							this.setSiteroot('')
							uni.removeStorageSync('siteroot')
							uni.reLaunch({
								url: '/pages/login/index'
							})
						}
					}
				})
			},
			edit() {
				this.$refs.uForm.validate().then(res => {
					if (this.userInfo.password && this.userInfo.password.length < 6) {
						return uni.$u.toast('新密码不得少于6位数');
					}
					this.beg.request({
						url: '/admin/routine.AdminInfo/edit',
						method: 'post',
						data: this.userInfo
					}).then(res => {
						if (res.code == 1) {
							uni.$u.toast(res.msg)
							setTimeout(() => {
								uni.removeStorageSync('token')
								uni.removeStorageSync('storeId')
								uni.reLaunch({
									url: '/pages/login/index'
								})
							}, 1000)
						}
					})
				}).catch(errors => {})
			},
			initCOS() {
				this.beg.request({
					url: '/admin/Cos/refreshToken',
					method: 'get',
				}).then(res => {
					// 获取临时密钥
					const {
						tmpSecretId,
						tmpSecretKey,
						sessionToken,
						startTime,
						expiredTime
					} = res.data;
					this.cos = new COS({
						getAuthorization: (options, callback) => {
							callback({
								TmpSecretId: tmpSecretId,
								TmpSecretKey: tmpSecretKey,
								SecurityToken: sessionToken,
								StartTime: startTime,
								ExpiredTime: expiredTime,
							});
						},
					});
				}).catch(error => {
					console.error('获取临时密钥失败:', error);
				});
			},
			getAdminInfo() {
				this.beg.request({
					url: '/admin/routine.AdminInfo/index'
				}).then(res => {
					if (res.code == 1) {
						res.data.info.avatar = siteroot.siteimgroot + res.data.info.avatar
						this.userInfo = res.data.info
					}
				})
			},
			async save() {
				if (!this.form.oldPassword) {
					return uni.$u.toast('请输入原密码');
				}
				if (!this.form.password) {
					return uni.$u.toast('请输入新密码');
				}
				if (!this.form.password_confirmation) {
					return uni.$u.toast('请再次输入新密码');
				}
				if (this.form.oldPassword && this.form.oldPassword.length < 6) {
					return uni.$u.toast('原密码不得少于6位数');
				}
				if (this.form.password && this.form.password.length < 6) {
					return uni.$u.toast('新密码少于6位数6位数');
				}
				if (this.form.password && this.form.password_confirmation && this.form.password != this.form
					.password_confirmation) {
					return uni.$u.toast('前后登录密码不一致');
				}
				let {
					msg,
					data,
					code,
				} = await this.beg.request({
					url: '/admin/routine.AdminInfo/edit',
					method: 'POST',
					data: this.form,
				})
				uni.$u.toast(msg)
				if (code && code == 200) {
					setTimeout(() => {
						this.show = false
						uni.removeStorageSync('token')
						uni.removeStorageSync('storeId')
						uni.reLaunch({
							url: '/pages/login/index'
						})
					}, 1000)
				}
			},
		}
	}
</script>

<style lang="scss">
</style>